﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gbL.NetBoard.Filters
{
    public class EntryFuncFilter : IEntryFilter<Func<IBlackboardEntry, bool>>
    {
        private object _filtersLock = new object();

        private Dictionary<object, IList<Func<IBlackboardEntry, bool>>> _filters = new Dictionary<object, IList<Func<IBlackboardEntry, bool>>>();

        protected virtual IList<Func<IBlackboardEntry, bool>> BuildFilterList()
        {
            return new List<Func<IBlackboardEntry, bool>>();
        }

        public void AddFilter(object owner, Func<IBlackboardEntry, bool> toFilter)
        {
            lock (_filtersLock)
            {
                if (!_filters.ContainsKey(owner)) _filters.Add(owner, BuildFilterList());
                _filters[owner].Add(toFilter);
            }
        }

        public void Precalculate(IBlackboardEntry toTest)
        {
            // not yet
        }

        public bool HasFilter(object owner)
        {
            return _filters.ContainsKey(owner);
        }

        public bool IsAllowed(object owner, IBlackboardEntry toTest)
        {
            lock (_filtersLock)
            {
                if (!HasFilter(owner)) return true;
                return _filters[owner].Any(x => x.Invoke(toTest));
            }
        }
    }
}
